import { type Point, TransformHandleType, type ViewTransform } from './types';

export class TransformHandle {
  public type: TransformHandleType;
  public position: Point;
  public size: number = 8;
  public color: string = '#ffffff';
  public strokeColor: string = '#3498db';
  public strokeWidth: number = 2;

  constructor(type: TransformHandleType, position: Point, size?: number) {
    this.type = type;
    this.position = { ...position };
    if (size) this.size = size;

    // 根据类型设置颜色
    this.setColorByType();
  }

  /**
   * 绘制控制柄
   */
  draw(ctx: CanvasRenderingContext2D, viewTransform: ViewTransform): void {
    ctx.save();

    // 应用视图变换
    ctx.translate(viewTransform.offset.x, viewTransform.offset.y);
    ctx.scale(viewTransform.scale, viewTransform.scale);
    ctx.rotate(viewTransform.rotation);

    // 计算控制柄在屏幕上的实际大小
    const handleSize = this.size / viewTransform.scale;
    const halfSize = handleSize / 2;

    // 设置样式
    ctx.fillStyle = this.color;
    ctx.strokeStyle = this.strokeColor;
    ctx.lineWidth = this.strokeWidth / viewTransform.scale;

    // 根据类型绘制不同的控制柄
    switch (this.type) {
      case TransformHandleType.MOVE:
        this.drawMoveHandle(ctx, this.position, halfSize);
        break;
      case TransformHandleType.ROTATE:
        this.drawRotateHandle(ctx, this.position, halfSize);
        break;
      case TransformHandleType.SCALE_X:
        this.drawScaleXHandle(ctx, this.position, halfSize);
        break;
      case TransformHandleType.SCALE_Y:
        this.drawScaleYHandle(ctx, this.position, halfSize);
        break;
      case TransformHandleType.SCALE_XY:
        this.drawScaleXYHandle(ctx, this.position, halfSize);
        break;
    }

    ctx.restore();
  }

  /**
   * 检查点是否在控制柄内
   */
  contains(point: Point, viewTransform: ViewTransform): boolean {
    const handleSize = this.size / viewTransform.scale;
    const halfSize = handleSize / 2;

    const dx = point.x - this.position.x;
    const dy = point.y - this.position.y;
    const distance = Math.sqrt(dx * dx + dy * dy);

    return distance <= halfSize;
  }

  /**
   * 设置控制柄位置
   */
  setPosition(position: Point): void {
    this.position = { ...position };
  }

  /**
   * 获取控制柄光标样式
   */
  getCursor(): string {
    switch (this.type) {
      case TransformHandleType.MOVE:
        return 'move';
      case TransformHandleType.ROTATE:
        return 'crosshair';
      case TransformHandleType.SCALE_X:
        return 'ew-resize';
      case TransformHandleType.SCALE_Y:
        return 'ns-resize';
      case TransformHandleType.SCALE_XY:
        return 'nwse-resize';
      default:
        return 'default';
    }
  }

  /**
   * 根据类型设置颜色
   */
  private setColorByType(): void {
    switch (this.type) {
      case TransformHandleType.MOVE:
        this.color = '#ffffff';
        this.strokeColor = '#3498db';
        break;
      case TransformHandleType.ROTATE:
        this.color = '#ffffff';
        this.strokeColor = '#e74c3c';
        break;
      case TransformHandleType.SCALE_X:
        this.color = '#ffffff';
        this.strokeColor = '#2ecc71';
        break;
      case TransformHandleType.SCALE_Y:
        this.color = '#ffffff';
        this.strokeColor = '#2ecc71';
        break;
      case TransformHandleType.SCALE_XY:
        this.color = '#ffffff';
        this.strokeColor = '#2ecc71';
        break;
    }
  }

  /**
   * 绘制移动控制柄
   */
  private drawMoveHandle(
    ctx: CanvasRenderingContext2D,
    position: Point,
    size: number,
  ): void {
    ctx.beginPath();
    ctx.arc(position.x, position.y, size, 0, 2 * Math.PI);
    ctx.fill();
    ctx.stroke();

    // 添加十字线
    ctx.beginPath();
    ctx.moveTo(position.x - size, position.y);
    ctx.lineTo(position.x + size, position.y);
    ctx.moveTo(position.x, position.y - size);
    ctx.lineTo(position.x, position.y + size);
    ctx.stroke();
  }

  /**
   * 绘制旋转控制柄
   */
  private drawRotateHandle(
    ctx: CanvasRenderingContext2D,
    position: Point,
    size: number,
  ): void {
    ctx.beginPath();
    ctx.arc(position.x, position.y, size, 0, 2 * Math.PI);
    ctx.fill();
    ctx.stroke();

    // 添加箭头
    ctx.beginPath();
    ctx.moveTo(position.x, position.y - size);
    ctx.lineTo(position.x - size * 0.3, position.y - size * 1.5);
    ctx.lineTo(position.x + size * 0.3, position.y - size * 1.5);
    ctx.closePath();
    ctx.fill();
  }

  /**
   * 绘制X轴缩放控制柄
   */
  private drawScaleXHandle(
    ctx: CanvasRenderingContext2D,
    position: Point,
    size: number,
  ): void {
    ctx.beginPath();
    ctx.rect(position.x - size, position.y - size / 2, size * 2, size);
    ctx.fill();
    ctx.stroke();

    // 添加箭头
    ctx.beginPath();
    ctx.moveTo(position.x - size * 1.5, position.y);
    ctx.lineTo(position.x + size * 1.5, position.y);
    ctx.stroke();
  }

  /**
   * 绘制Y轴缩放控制柄
   */
  private drawScaleYHandle(
    ctx: CanvasRenderingContext2D,
    position: Point,
    size: number,
  ): void {
    ctx.beginPath();
    ctx.rect(position.x - size / 2, position.y - size, size, size * 2);
    ctx.fill();
    ctx.stroke();

    // 添加箭头
    ctx.beginPath();
    ctx.moveTo(position.x, position.y - size * 1.5);
    ctx.lineTo(position.x, position.y + size * 1.5);
    ctx.stroke();
  }

  /**
   * 绘制XY轴缩放控制柄
   */
  private drawScaleXYHandle(
    ctx: CanvasRenderingContext2D,
    position: Point,
    size: number,
  ): void {
    ctx.beginPath();
    ctx.rect(position.x - size, position.y - size, size * 2, size * 2);
    ctx.fill();
    ctx.stroke();

    // 添加对角线
    ctx.beginPath();
    ctx.moveTo(position.x - size, position.y - size);
    ctx.lineTo(position.x + size, position.y + size);
    ctx.moveTo(position.x + size, position.y - size);
    ctx.lineTo(position.x - size, position.y + size);
    ctx.stroke();
  }
}

export default TransformHandle;

export { TransformHandleType };
